package com.peng.leetcode.tree;

/**
 * DiameterOfBinaryTree
 * 543. 二叉树的直径
 * https://leetcode.cn/problems/diameter-of-binary-tree/
 * <p>
 * Created on 2022/12/1
 *
 * @author lupeng
 */
public class DiameterOfBinaryTree1 {
    /**
     * 自底向上
     */
    int ans = Integer.MIN_VALUE;

    public int diameterOfBinaryTree(TreeNode root) {
        if (root == null) {
            return 0;
        }
        dfs(root);
        return ans;
    }

    int dfs(TreeNode n) {
        if (n == null) {
            return 0;
        }
        int l = dfs(n.left);
        int r = dfs(n.right);
        ans = Math.max(ans, l + r);
        return Math.max(l, r) + 1;
    }


}
